
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    Image,
    View,
    Button,
    TextInput,
    TouchableOpacity
} from 'react-native';
import { AsyncStorage } from "react-native"
export default class Page1 extends React.Component {


  constructor(props) {
    super(props);
    this.state = {
      value: "",
      data:""
    };
  }

    async _dosave(){
      try {
        await AsyncStorage.setItem('@tj:key', 'I like to save it.');
      } catch (error) {
        // Error saving data
        console.warn('err save')
      }
    }

    _doget= async () => {
      try {
        const value=await AsyncStorage.getItem('@tj:key');
        this.setState({
            value:value
        })
      } catch (error) {
        // Error saving data
        console.warn('err save')
      }
    }

        _getAll = () => {
            this.setState({data: '\n'});
            AsyncStorage.getAllKeys((err,keys) => {
                if (keys && keys.length>0) {
                    keys.map((key, index) => {
                        AsyncStorage.getItem(key, (err, result) => {
                            var msg = this.state.data+key + ': ' + result + '\n';
                            this.setState({data: msg});
                        })
                    });
                }
            })
        }

      _Multisave() {
        //设置多项
        var keyValuePairs = [['name', '123'], ['phone', '456']]
        AsyncStorage.multiSet(keyValuePairs, function(errs){
          if(errs){
            //TODO：存储出错
            return;
          }
          alert('数据保存成功!');
        });
      }

  _getMulti(){

    //需要查询的键值
    var keys = ["name","phone"];
    //根据键数组查询保存的键值对
    AsyncStorage.multiGet(keys, function(errs, result){
      //如果发生错误，这里直接返回（return）防止进入下面的逻辑
      if(errs){
        return;
      }

      //得到的结果是二维数组（result[i][0]表示我们存储的键，result[i][1]表示我们存储的值）
//      _that.setState({
//        name: (result[0][1]!=null)?result[0][1]:'',
//        phone: (result[1][1]!=null)?result[1][1]:''
//      });
        console.warn(result[0][1])
        console.warn(result[1][1])
    });
  }



    render() {
        return (<View >
            <Text >欢迎来到Page1</Text>
            <Button
                title={"save"}
                onPress={() => {
                    this._dosave()
//                    navigation.goBack();
                }}
            />
            <Button
                title={"get"}
                onPress={() => {
                    this._doget()
                }}
            />
            <Button
                title={"getAll"}
                onPress={() => {
                    this._getAll()

                }}
            />
            <Button
                title={"setMutil"}
                onPress={() => {
                    this._Multisave()

                }}
            />
            <Button
                title={"getMutil"}
                onPress={() => {
                    this._getMulti()

                }}
            />
            <Text>
            {this.state.value}
            </Text>
             <Text>data:{this.state.data}</Text>
        </View>)
    }
}